<?php

namespace app\admin\controller;

use think\Db;
use User;

/**
 * 用户留言
 * @author yao
 *
 */
class Message extends Home
{
    private static $liuyan;

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 公司留言
     */
    public function liuyan()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $touser = $data['touser'];
            $tuser = Db('member')->where('usernumber',$touser)->find();
            if(!$tuser){
                $this->error('收件人不存在');
            }
            if(!$data['content']){
                $this->error('请输入信息内容');
            }
            $data['fromuserid'] = 0;
            $data['touser'] = $tuser['uid'];
            $data['create_time'] = time();
            $data['status'] = 1;
            $res = Db('liuyan')->insert($data);
            if ($res) {
                $this->success('留言成功', url('liuyanList', array('type' => 2)));
            } else {
                $this->error('留言失败');
            }
        } else {
            $this->assign('title', '写信息');
            return $this->fetch();
        }
    }

    /**
     * 留言列表
     */
    public function liuyanList()
    {
        $type = input('type', 1);
        $maps['begintime'] = $starttime = input('begintime', date("Y-m-d", strtotime('-12 month')));
        $maps['endtime'] = $endtime = input('endtime', date("Y-m-d", time()));
        $maps['usernumber'] = $usernumber = input('usernumber');
        $map['status'] = array('egt', 0);
        $starttime = strtotime("$starttime 00:00:00");
        $endtime = strtotime("$endtime 23:59:59");

        $maps['type'] = $type;
        $map['create_time'] = array(array('gt', $starttime), array('lt', $endtime));
        if (!empty($usernumber)) {
            $numwhere['usernumber'] = $usernumber;
            $user = Db::name('Member')->where($numwhere)->find();
            if (!$user) {
                $this->error('用户不存在！');
            }
        }

        if ($type == 1) {
            /*收件箱*/
            $list = Db('liuyan')->where($map)->where("touser = 0 and status = 1")->whereOr("fromuserid = 0 and status = 2")->order('create_time desc')->paginate(20);
            $this->assign('title', '发件箱');
        }
        if ($type == 2) {
            /*发件箱*/
            $list = Db('liuyan')->where($map)->where("touser = 0 and status = 2")->whereOr("fromuserid = 0 and status = 1")->order('create_time desc')->paginate(20);
            $this->assign('title', '收件箱');
        }

        $page = $list->render();
        $this->assign('_list', $list);
        $this->assign('page', $page);

        $this->searchCondition($maps);
        $this->assign('type', $type);

        return $this->fetch('liuyanlist');
    }

    /**
     * 留言回复
     */
    public function liuyanReply()
    {
        $id = input('id');
        $liuyan = Db('Liuyan')->where('id' , $id)->find();
        if (request()->isPost()) {
            $data = input('post.');
            if(!$data['reply']){
                $this->error('请输入回复内容');
            }
            $data['reply_time'] = time();
            $data['id'] = $id;
            $data['status'] = 2;
            $res = Db('Liuyan')->update($data);
            if ($res) {
                $this->success('回复成功', url('liuyanList', array('type' => 1)));
            } else {
                $this->error('回复失败');
            }
        } else {
            $this->assign('liuInfo', $liuyan);
            $this->assign('title', '留言回复');
            return $this->fetch('liuyanreply');
        }
    }

    /**
     * 留言内容
     */
    public function liuyanContent()
    {
        $id = I('id');
        $type = I('type');
        $content = M('Liuyan')->where(array('id' => $id))->find();
        $this->assign('content', $content);
        $this->assign('type', $type);

        $this->display();
    }

    /**
     * 删除留言信息
     */
    public function deleteLiuyan()
    {
        $id = I('id');
        $map['id'] = $id;
        $type = I('type');
        $status = I('status');
        if ($type == 1) {
            $res = M('Liuyan')->where($map)->setField('tostatus', 0);
        }
        if ($type == 2) {
            if ($status == 0) {
                $res = M('Liuyan')->where($map)->delete();
            } else {
                $res = M('Liuyan')->where($map)->setField('fromstatus', 0);
            }
        }
        if ($res) {
            $this->success('删除成功', U('liuyanList', array('type' => $type)));
        } else {
            $this->error('删除失败', U('liuyanList', array('type' => $type)));
        }
    }
}